package hk.m4s.lr.repair.test.ui.user;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.JiangliModel;
import hk.m4s.lr.repair.test.model.MyOrderModel;
import hk.m4s.lr.repair.test.model.MyWaiteOrderModel;
import hk.m4s.lr.repair.test.model.OrderDetailModel;
import hk.m4s.lr.repair.test.ui.adapter.CancleAdapter;
import hk.m4s.lr.repair.test.ui.adapter.CancleOrderListAdapter;
import hk.m4s.lr.repair.test.views.listview.InnerListView;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/5/4.
 * 取消订单
 */

public class CancalOrderActivity extends UeBaseActivity {
    private RelativeLayout show_layout;
    private TextView order_shop_name;
    private TextView order_shop_price;
    private TextView order_shop_size;
    private TextView order_shop_num, cancle_text;
    private InnerListView cancelList;
    private ImageView order_shop_img;
    private Button cancle_order;
    private MyOrderModel myOrderModel;
    private Context context;
    private ListView jiangli_list;
    private List<JiangliModel.ListBean> jiangliList = new ArrayList<>();
    private CancleOrderListAdapter jiangliListAdapter;
    private String jiangliName = "";
    MyOrderModel.ListBean model;
    private CancleAdapter adapter;
    private List<MyWaiteOrderModel> myjiangLiModelList;
    private List<OrderDetailModel.GoodsInfoBean> modelList = new ArrayList<>();
    String type;
    String orderId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_cancle_order);
        hiddenFooter();
        showGoBackBtn();
        context = this;
        setTitleText("取消订单");

        findbaseView();
    }

    public void findbaseView() {
        order_shop_name = (TextView) findViewById(R.id.order_shop_name);
        order_shop_price = (TextView) findViewById(R.id.order_shop_price);
        order_shop_size = (TextView) findViewById(R.id.order_shop_size);
        order_shop_num = (TextView) findViewById(R.id.order_shop_num);
        cancelList = (InnerListView) findViewById(R.id.cancelList);
        cancle_text = (TextView) findViewById(R.id.cancle_text);
        cancle_order = (Button) findViewById(R.id.cancle_ok);
        jiangli_list = (ListView) findViewById(R.id.jiangli_list);
        order_shop_img = (ImageView) findViewById(R.id.order_shop_img);
        show_layout = (RelativeLayout) findViewById(R.id.show_layout);
        type = getIntent().getStringExtra("type");
//        if(type.equals("1")) {
////            model = (MyOrderModel.ListBean) getIntent().getSerializableExtra("model");
////            orderId=model.getOrder_id();
////            if (model.getDetail() != null) {
////                myjiangLiModelList = new ArrayList<>();
////                for (MyOrderModel.ListBean.DetailBean detailBean : model.getDetail()) {
////                    MyWaiteOrderModel waiteOrderModel = new MyWaiteOrderModel();
////                    waiteOrderModel.setTime(AppTools.getStrTime(detailBean.getAddTime()));
////                    waiteOrderModel.setGoods_id(detailBean.getGoods_id());
////                    waiteOrderModel.setName(detailBean.getGoods_name());
////                    waiteOrderModel.setPrice(detailBean.getGoods_price());
////                    waiteOrderModel.setSize(detailBean.getGoods_color());
////                    waiteOrderModel.setBuyNum(detailBean.getGoods_num());
////                    waiteOrderModel.setState(detailBean.getStatus());
////                    waiteOrderModel.setImg(detailBean.getImg());
////                    myjiangLiModelList.add(waiteOrderModel);
////                }
////                adapter = new CancleAdapter(context, myjiangLiModelList);
////                cancelList.setAdapter(adapter);
////            }
//        }else{
//            myjiangLiModelList = new ArrayList<>();
//            orderId=getIntent().getStringExtra("orderId");
//            modelList=(List<OrderDetailModel.GoodsInfoBean>) getIntent().getSerializableExtra("model");
//            for (OrderDetailModel.GoodsInfoBean detailBean : modelList) {
//                MyWaiteOrderModel waiteOrderModel = new MyWaiteOrderModel();
//                waiteOrderModel.setTime(AppTools.getStrTime(detailBean.getAddTime()));
//                waiteOrderModel.setGoods_id(detailBean.getGoods_id());
//                waiteOrderModel.setName(detailBean.getGoods_name());
//                waiteOrderModel.setPrice(detailBean.getGoods_price());
//                waiteOrderModel.setSize(detailBean.getGoods_color());
//                waiteOrderModel.setBuyNum(detailBean.getGoods_num());
//                waiteOrderModel.setState(detailBean.getStatus());
//                waiteOrderModel.setImg(detailBean.getImg());
//                myjiangLiModelList.add(waiteOrderModel);
//            }
//            adapter = new CancleAdapter(context, myjiangLiModelList);
//            cancelList.setAdapter(adapter);
//        }
        adapter = new CancleAdapter(context, myjiangLiModelList);
        cancelList.setAdapter(adapter);
        jiangliListAdapter = new CancleOrderListAdapter(context, jiangliList);
        jiangli_list.setAdapter(jiangliListAdapter);
//        try {
//            Glide.with(context).load(myOrderModel.getImg()).asBitmap().placeholder(R.mipmap.upload_photo).error(R.mipmap.upload_photo).into(order_shop_img);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        //cancelOrderReason();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancle_ok:
                //updateOrderStatus();
                break;
            case R.id.clcik_order:
                show_layout.setVisibility(View.VISIBLE);
                cancle_order.setVisibility(View.GONE);
                break;
            case R.id.btn_cancle:
                show_layout.setVisibility(View.GONE);
                cancle_order.setVisibility(View.VISIBLE);
                jiangliName = "";
                break;
            case R.id.btn_sure:
                show_layout.setVisibility(View.GONE);
                cancle_order.setVisibility(View.VISIBLE);
                for (JiangliModel.ListBean model : jiangliList) {
                    if (model.getState().equals("1")) {
                        jiangliName = model.getText();
                    }
                }
                cancle_text.setText(jiangliName);
                break;
        }
    }

//    public void cancelOrderReason() {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        AccountSerive.cancelOrderReason(context, parmas, new ResponseCallback<JiangliModel>() {
//            @Override
//            public void onSuccess(JiangliModel model) {
//                if (model.getList() != null) {
//                    jiangliList.addAll(model.getList());
//                    jiangliListAdapter.notifyDataSetChanged();
//                }
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
//
//    }

//    public void updateOrderStatus() {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        parmas.put("orderId", orderId);
//        parmas.put("reason", jiangliName);
//        parmas.put("status", "5");
//        AccountSerive.updateOrderStatus(context, parmas, new ResponseCallback<JiangliModel>() {
//            @Override
//            public void onSuccess(JiangliModel model) {
//                if (type.equals("2")) {
//                    if (!LookPayMoneyOrderActivity.instance.isFinishing()) {
//                        finish();
//                    }
//                }
//                finish();
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
//
//    }
}
